package com.aldd.controller;

import com.aldd.entity.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class BookController {
    //跳转
    @RequestMapping("getString") //http://localhost/getString
    public String getString() {
        return "index.jsp";
    }

    //返回字符串
    @RequestMapping("getStr")
    @ResponseBody //http://localhost/getStr
    public String getStr() {
        return "666";
    }

    //返回对象
    @RequestMapping("getBook")
    @ResponseBody //http://localhost/getBook
    public Book getBook() {
        Book book = new Book(1, "小葵花");
        return book;
    }

    //接收普通参数
    @RequestMapping(value = "getString1", produces = "application/json;charset=utf-8")//解决返回乱码
    @ResponseBody //http://localhost/getString1?user=李四&age=20
    public String getString1(@RequestParam(value = "user", required = false, defaultValue = "张三") String userName, @RequestParam(required = false, defaultValue = "18") int age) {
        return userName + "今年" + age + "岁了";
    }

    //接收对象
    @RequestMapping("getBook1")
    @ResponseBody //http://localhost/getBook1?bookId=1&bookName=小葵花
    public Book getBook1(Book book) {
        return book;
    }

    //接收集合
    @RequestMapping("getHobby")
    @ResponseBody //http://localhost/getHobby?hobby=唱&hobby=跳&hobby=rap&hobby=篮球
    public String[] getHobby(String[] hobby) {
        return hobby;
    }

    //接收json的集合
    @RequestMapping("getBookList")
    @ResponseBody //http://localhost/getBookList
    public List<Book> getBookList(@RequestBody List<Book> bookList) {//用json发送
        return bookList;
    }
}
